package com.astronomy.qa.service;

import com.astronomy.entity.IdWorker;
import com.astronomy.entity.PageResult;
import com.astronomy.qa.mapper.ProblemMapper;
import com.astronomy.qa.pojo.Problem;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProblemService {

    @Autowired
    private ProblemMapper problemMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 全部数据
     *
     * @return
     */
    public List<Problem> findAll() {
        return problemMapper.selectAll();
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public Problem findById(String id) {
        return problemMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加
     *
     * @param problem
     */
    public void add(Problem problem) {
        problem.setId(idWorker.nextId()+"");
        problemMapper.insert(problem);
    }

    /**
     * 修改
     *
     * @param problem
     */
    public void update(Problem problem) {
        problemMapper.updateByPrimaryKey(problem);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(String id) {
        problemMapper.deleteByPrimaryKey(id);
    }


    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    public Page<Problem> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Problem>) problemMapper.selectAll();
    }

    /**
     * 条件查询
     *
     * @param searchMap
     * @return
     */
    public List<Problem> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return problemMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Problem.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // ID
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                criteria.andLike("id", "%" + searchMap.get("id") + "%");
            }
            // 标题
            if (searchMap.get("title") != null && !"".equals(searchMap.get("title"))) {
                criteria.andLike("title", "%" + searchMap.get("title") + "%");
            }
            // 内容
            if (searchMap.get("content") != null && !"".equals(searchMap.get("content"))) {
                criteria.andLike("content", "%" + searchMap.get("content") + "%");
            }
            // 用户ID
            if (searchMap.get("userid") != null && !"".equals(searchMap.get("userid"))) {
                criteria.andLike("userid", "%" + searchMap.get("userid") + "%");
            }
            // 昵称
            if (searchMap.get("nickname") != null && !"".equals(searchMap.get("nickname"))) {
                criteria.andLike("nickname", "%" + searchMap.get("nickname") + "%");
            }
            // 是否解决
            if (searchMap.get("solve") != null && !"".equals(searchMap.get("solve"))) {
                criteria.andLike("solve", "%" + searchMap.get("solve") + "%");
            }
            // 回复人昵称
            if (searchMap.get("replyname") != null && !"".equals(searchMap.get("replyname"))) {
                criteria.andLike("replyname", "%" + searchMap.get("replyname") + "%");
            }


        }
        return example;
    }

    /**
     * 条件+分页查询
     *
     * @param searchMap 查询条件
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    public PageResult<Problem> findPage(Map<String, Object> searchMap, int page, int size) {
        Example example = createExample(searchMap);
        PageHelper.startPage(page, size);
        List<Problem> problems = problemMapper.selectByExample(example);
        PageInfo<Problem> problemPageInfo = new PageInfo<>(problems);
        return new PageResult<>(problemPageInfo.getTotal(), problemPageInfo.getList());
    }

    /**
     * 最新问答列表
     * @param labelid 标签id
     * @param page 页码
     * @param size 页大小
     * @return
     */
    public PageResult<Problem> newList(String labelid, int page, int size) {
        //开启分页
        PageHelper.startPage(page, size);
        //查询结果
        List<Problem> problems = problemMapper.newList(labelid);
        //把结果封装成PageInfo
        PageInfo<Problem> problemPageInfo = new PageInfo<>(problems);
        //返回结果
        return new PageResult<>(problemPageInfo.getTotal(), problemPageInfo.getList());
    }

    /**
     * 热门回答列表
     * @param labelid 标签id
     * @param page 页码
     * @param size 页大小
     * @return
     */
    public PageResult<Problem> hotList(String labelid, int page, int size) {
        PageHelper.startPage(page, size);
        List<Problem> problems = problemMapper.hotList(labelid);
        PageInfo<Problem> problemPageInfo = new PageInfo<>(problems);
        return new PageResult<>(problemPageInfo.getTotal(), problemPageInfo.getList());
    }
    /**
     * 最新回答列表
     * @param labelid 标签id
     * @param page 页码
     * @param size 页大小
     * @return
     */
    public PageResult<Problem> waitList(String labelid, int page, int size) {
        PageHelper.startPage(page, size);
        List<Problem> problems = problemMapper.waitList(labelid);
        PageInfo<Problem> problemPageInfo = new PageInfo<>(problems);
        return new PageResult<>(problemPageInfo.getTotal(), problemPageInfo.getList());
    }

}
